﻿using InterfaceService;
using Model.Entity;
using Model.Request;
using Model.Response;
using SqlSugar;

namespace Service
{
    public class UserService : IUserService
    {
        private ISqlSugarClient _db { get; set; }

        public UserService(ISqlSugarClient db)
        {
            _db = db;
        }

        public async Task<UserRes> GetUser(LoginReq req)
        {
            var user = await _db.Queryable<Users>()
                .Where(x => x.Name == req.UserName && x.Password == req.Password)
                //1、属性手动赋值
                //2、AutoMapper
                //3、通过SqlSugar自带的功能实现,true表示Users表中和UserRes类中字段若一致则自动赋值
                .Select(p => new UserRes { }, true).FirstAsync();

            return user;
        }

        public async Task<bool> EditNickNameOrPassword(string userId, PersonEdit req)
        {
            var info = await _db.Queryable<Users>().FirstAsync(p => p.Id == userId);
            if (info != null) 
            {
                if(!string.IsNullOrEmpty(req.NickName))
                    info.NickName = req.NickName;

                if(!string.IsNullOrEmpty(req.Password))
                    info.Password = req.Password;

                return await _db.Updateable(info).ExecuteCommandHasChangeAsync();
            }

            return false;
        }
    }
}
